import net.es.lookup.client.RecordManager;
import net.es.lookup.client.SimpleLS;
import net.es.lookup.common.exception.LSClientException;
import net.es.lookup.common.exception.ParserException;
import net.es.lookup.records.ErrorRecord;
import net.es.lookup.records.Record;

/**
 * Created with IntelliJ IDEA.
 * User: luke
 * Date: 7/7/13
 * Time: 6:23 PM
 * To change this template use File | Settings | File Templates.
 */
public class ExampleRecordManager {

        /*
    This example demonstrates the use of the RecordManager. The purpose of the
    RecordManager is to provide methods for performing operations on records
    registered with the Simple Lookup Service. The supported operations
    include renew, delete, access, and access a key/value pair.
     */

    public static void main(String[] args) {

        String host = "localhost";
        int port = 8090;
        String recordURI = "record-type/record-ID";

        try {
            /*
            An instance of SimpleLS first needs to be instantiated. This will
            be passed to the RecordManager constructor.
            */
            SimpleLS sls = new SimpleLS(host, port);
            RecordManager rm = new RecordManager(sls);

            /*
            All RecordManager operations require a record URI of the format
            "<record-type>/<record-ID>". This URI is set using the
            setRecordUri method.
             */
            rm.setRecordUri(recordURI);

            /*
            Alternatively, the record URI can be passed to the RecordManager
            in the constructor arguments.
             */
            rm = new RecordManager(sls, recordURI);

            /*
            The RecordManager uses the renew method to perform the renew
            operation on the record specified by the record URI. The method
            returns an instance of Record if the record is successfully
            renewed.
             */
            Record renewedRecord;
            renewedRecord = rm.renew();

            /*
            The RecordManager uses the getRecord method to perform the access
            operation on the record specified by the record URI. The method
            returns an instance of Record if the record is successfully
            accessed.
             */
            Record accessedRecord;
            accessedRecord = rm.getRecord();

            /*
            The RecordManager uses the getKeyValueInRecord method to perform
            the access key/value pair operation on the record specified by the
            record URI. The method returns an instance of Record if the
            key/value pair is successfully accessed.
             */
            Record keyValueRecord;
            keyValueRecord = rm.getKeyValueInRecord("key");

            /*
            The RecordManager uses the delete method to perform the delete
            operation on the record specified by the record URI. The method
            returns an instance of Record if the record is successfully
            deleted.
             */
            Record deletedRecord;
            deletedRecord = rm.delete();

            /*
            If the operation fails, the returned record will be an instance of
            the ErrorRecord class. This class provides means of retrieving
            both the HTTP response and response code.
             */
            String response = ((ErrorRecord) renewedRecord).getErrorMessage();
            int responseCode = ((ErrorRecord) renewedRecord).getErrorCode();

        } catch (LSClientException e) {

            /*
            Both the SimpleLS and RecordManager throw LSClientExceptions.
             */
        } catch (ParserException e) {

            /*
            The RecordManager throws a ParserException if the record data
            returned by the specified SimpleLS cannot be parsed to a Record.
             */
        }
    }
}
